package com.zimmur.app.mocolink.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zimmur.app.mocolink.dao.UserCollectionMapper;
import com.zimmur.app.mocolink.domain.common.Constant;
import com.zimmur.app.mocolink.domain.model.Goods;
import com.zimmur.app.mocolink.domain.model.GroupGoods;
import com.zimmur.app.mocolink.domain.model.UserCollection;
import com.zimmur.app.mocolink.domain.model.UserCollectionExample;
import com.zimmur.app.mocolink.domain.util.PageUtil;
import com.zimmur.app.mocolink.domain.vo.GoodsCollectionInfo;
import com.zimmur.app.mocolink.domain.vo.GroupCollectionInfo;
import com.zimmur.app.mocolink.service.IGoodsService;
import com.zimmur.app.mocolink.service.IGroupGoodsService;
import com.zimmur.app.mocolink.service.IUserCollectionService;

@Service
public class UserCollectionServiceImpl implements IUserCollectionService {
	@Autowired
	private UserCollectionMapper userCollectionMapper;
	@Autowired
	private IGoodsService goodsService;
	@Autowired
	private IGroupGoodsService groupGoodsService;
	@Override
	public int delete(String[] collectIds) {
		List<Integer> ids =new ArrayList<>();
		int flag = 0;
		for(String collectId:collectIds){
			Integer id = Integer.valueOf(collectId);
			ids.add(id);
		}
		if(ids.size()>0){
			UserCollectionExample example = new UserCollectionExample();
			example.createCriteria().andIdIn(ids);
			flag = userCollectionMapper.deleteByExample(example);
		}
		return flag;
	}

	@Override
	public int collectGoods(Integer goodsId, Integer userId) {
		Goods goods = goodsService.selectById(goodsId);
		if(goods!=null){
			UserCollection collection = userCollectionMapper.selectByUserIdAndDataId(userId, goodsId, Constant.Collect_Goods_Type);
			int collectNum = goods.getCollectionNum()==null?0:goods.getCollectionNum();
			if(collection!=null){
				if(collection.getIsCollect()==0){
					collectNum++;
					collection.setIsCollect(Constant.byte_1);
				}else{
					collectNum--;
					collection.setIsCollect(Constant.byte_0);
				}
				userCollectionMapper.updateByPrimaryKeySelective(collection);
			}else{
				collection = new UserCollection();
				collection.setCoverImage(goods.getCoverImage());
				collection.setDataId(goodsId);
				collection.setIsCollect(Constant.byte_1);
				collection.setName(goods.getGoodsName());
				collection.setPrice(goods.getPrice());
				collection.setType(Constant.Collect_Goods_Type);
				collection.setUserId(userId);
				userCollectionMapper.insertSelective(collection);
				collectNum++;
			}
			goods.setCollectionNum(collectNum);
			goodsService.update(goods);
			return 1;
		}
		return -1;
	}

	@Override
	public Map<String,Integer> collectGroup(Integer groupGoodsId, Integer userId) {
		GroupGoods groupGoods = groupGoodsService.selectById(groupGoodsId);
		if(groupGoods!=null){
			Map<String,Integer> map = new HashMap<String, Integer>();
			UserCollection collection = userCollectionMapper.selectByUserIdAndDataId(userId, groupGoodsId, Constant.Collect_Group_Type);
			int collectNum = groupGoods.getCollectQuantity()==null?0:groupGoods.getCollectQuantity();
			int status=1;
			if(collection!=null){
				if(collection.getIsCollect()==0){
					collectNum++;
					collection.setIsCollect(Constant.byte_1);
				}else{
					collectNum--;
					collection.setIsCollect(Constant.byte_0);
					status=0;
				}
				userCollectionMapper.updateByPrimaryKeySelective(collection);
			}else{
				collection = new UserCollection();
				collection.setCoverImage(groupGoods.getCoverImage());
				collection.setDataId(groupGoodsId);
				collection.setIsCollect(Constant.byte_1);
				collection.setType(Constant.Collect_Group_Type);
				collection.setUserId(userId);
				userCollectionMapper.insertSelective(collection);
				collectNum++;
			}
			map.put("collectionQuantity", collectNum);
			map.put("status", status);
			groupGoods.setCollectQuantity(collectNum);
			groupGoodsService.update(groupGoods);
			return map;
		}
		return null;
	}

	@Override
	public PageUtil<GroupCollectionInfo> queryGroupByUserId(Integer userId, Integer pageNo) {
		PageUtil<GroupCollectionInfo> page = new PageUtil<>();
		page.setPageNo(pageNo);
		List<GroupCollectionInfo> list = userCollectionMapper.selectGroupCollectionByUserId(userId, page.getPageBeginNo(), page.getPageSize());
		int count = userCollectionMapper.countCollectionByUserId(userId, Constant.Collect_Group_Type);
		page.setItemList(list);
		page.setTotalRecord(count);
		return page;
	}

	@Override
	public PageUtil<GoodsCollectionInfo> queryGoodsByUserId(Integer userId, Integer pageNo) {
		PageUtil<GoodsCollectionInfo> page = new PageUtil<>();
		page.setPageNo(pageNo);
		List<GoodsCollectionInfo> list = userCollectionMapper.selectGoodsCollectionByUserId(userId, page.getPageBeginNo(), page.getPageSize());
		int count = userCollectionMapper.countCollectionByUserId(userId, Constant.Collect_Goods_Type);
		page.setItemList(list);
		page.setTotalRecord(count);
		return page;
	}

}
